<template>
  <el-dialog v-model="dialog.visible" title="充值" width="30%" @close="handleCloseDialog">
    <div class="form-container">
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="150px"
        class="form mb-0"
      >
        <el-form-item label="金额" prop="amount">
          <el-input v-model.number.trim="formData.amount" type="number" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="充值时间" prop="rechargeTime">
          <el-date-picker
            v-model="formData.rechargeTime"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择充值时间"
          />
        </el-form-item>
        <el-form-item label="付款凭证" prop="certificate">
          <MultiImageUpload v-model="formData.certificate" />
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="handleCloseDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import { ElMessage } from "element-plus";
import MultiImageUpload from "@/components/Upload/MultiImageUpload.vue";
import coinsAPI, { CoinsRechargeRecodeForm } from "@/api/coins/coins-api";
import { TRANSACTION_TYPE_ENUM } from "@/enums/conins/ConinsEnum";
// 定义 emit，用于通知外部关闭事件
const emit = defineEmits(["update:modelValue", "loadData"]);
const dialog = reactive({
  title: "",
  visible: false,
});
/*基础信息*/
const formRef = ref();
const currentRow = ref({});

const defaultFormData: CoinsRechargeRecodeForm = {
  amount: undefined,
  applyUserId: 0,
  certificate: [],
  coins: 0,
  companyId: 0,
  createBy: "",
  createTime: "",
  enterType: undefined,
  id: undefined,
  merchantUserId: 0,
  rechargeTime: "",
  remark: "",
  sort: 0,
  status: undefined,
  transactionId: "",
  type: TRANSACTION_TYPE_ENUM.RECHARGE.value,
  updateBy: "",
  updateTime: "",
};
const formData = ref({
  ...defaultFormData,
});
const formRules = reactive({
  amount: [{ required: true, message: "请输入金额", trigger: "blur" }],
  certificate: [{ required: true, message: "请上传付款凭证", trigger: "change" }],
  rechargeTime: [{ required: true, message: "请选择充值时间", trigger: "change" }],
});

async function handleSubmit() {
  const validate = await formRef.value.validate();
  if (!validate) return;
  try {
    await coinsAPI.add({
      ...formData.value,
      certificate: JSON.stringify(formData.value.certificate),
    });
    ElMessage.success("成功");
    emit("loadData");
    handleCloseDialog();
  } catch (error) {
    console.log("error", error);
    ElMessage.error("失败");
  }
}

// 关闭弹窗
function handleCloseDialog() {
  dialog.visible = false;
  emit("update:modelValue", false); // 关闭弹窗
}

function showModal(val) {
  dialog.visible = true;
  currentRow.value = val;
  Object.assign(formData.value, defaultFormData, {
    certificate: JSON.parse(val.certificate || "[]"),
  });
}

defineExpose({
  showModal,
});
</script>
<style style="scss" scoped>
.form-container {
  height: 500px;
  overflow: auto;
}
.form {
  ::v-deep(.el-form-item) {
    margin-bottom: 0;
  }
}
</style>
